#include<iostream>
const int N = 1e5 + 10;
int pre[N], ne[N], e[N],mp[N],h, p, id;
using namespace std;
void push_front(int x)
{
	id++;
	e[id] = x;
	mp[x] = id;
	pre[id] = h;
	ne[id] = ne[h];
	pre[ne[h]] = id;
	ne[h] = id;
}
void insert_b(int p, int x)
{
	id++;
	e[id] = x;
	mp[x] = id;
	pre[id] = p;
	ne[id] = ne[p];
	pre[ne[p]] = id;
	ne[p] = id;
}
int find(int x)
{
	return mp[x];
}
void print()
{
	for (int i = ne[h]; i != 0; i = ne[i])
	{
		cout << e[i] << ' ';
	}
	cout << endl << endl;
}
void insert_f(int p, int x)
{
	id++;
	e[id] = x;
	mp[x] = id;
	pre[id] = pre[p];
	ne[id] = p;
	ne[pre[p]] = id;
	pre[p] = id;
}
void erase(int x)
{
	ne[pre[x]] = ne[x];
	pre[ne[x]] = pre[x];
	mp[e[p]] = 0;
}
int main()
{
	for (int i = 1; i <= 5; i++)
	{
		push_front(i);
		print();
	}
	/*cout << find(3) << endl;*/
	insert_b(4, 99);
	print();
	insert_f(2, 23);
	print();
	erase(2);
	print();
	return 0;
}